package org.jivesoftware.smackx.pubsub;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import org.jivesoftware.smack.Connection;
import org.jivesoftware.smack.PacketListener;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.filter.OrFilter;
import org.jivesoftware.smack.filter.PacketFilter;
import org.jivesoftware.smack.packet.IQ.Type;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smack.packet.PacketExtension;
import org.jivesoftware.smackx.Form;
import org.jivesoftware.smackx.packet.DelayInformation;
import org.jivesoftware.smackx.packet.DiscoverInfo;
import org.jivesoftware.smackx.packet.Header;
import org.jivesoftware.smackx.packet.HeadersExtension;
import org.jivesoftware.smackx.pubsub.listener.ItemDeleteListener;
import org.jivesoftware.smackx.pubsub.listener.ItemEventListener;
import org.jivesoftware.smackx.pubsub.listener.NodeConfigListener;
import org.jivesoftware.smackx.pubsub.packet.PubSub;
import org.jivesoftware.smackx.pubsub.packet.PubSubNamespace;
import org.jivesoftware.smackx.pubsub.packet.SyncPacketSend;
import org.jivesoftware.smackx.pubsub.util.NodeUtils;

public abstract class Node
{
  protected Connection con;
  protected ConcurrentHashMap<NodeConfigListener, PacketListener> configEventToListenerMap;
  protected String id;
  protected ConcurrentHashMap<ItemDeleteListener, PacketListener> itemDeleteToListenerMap;
  protected ConcurrentHashMap<ItemEventListener, PacketListener> itemEventToListenerMap = new ConcurrentHashMap();
  protected String to;

  Node(Connection paramConnection, String paramString)
  {
    this.itemDeleteToListenerMap = new ConcurrentHashMap();
    this.configEventToListenerMap = new ConcurrentHashMap();
    this.con = paramConnection;
    this.id = paramString;
  }

  private static List<String> getSubscriptionIds(Packet paramPacket)
  {
    Iterator localIterator;
    HeadersExtension localHeadersExtension = (HeadersExtension)paramPacket.getExtension("headers", "http://jabber.org/protocol/shim");
    ArrayList localArrayList = null;
    if (localHeadersExtension != null)
    {
      localArrayList = new ArrayList(localHeadersExtension.getHeaders().size());
      localIterator = localHeadersExtension.getHeaders().iterator();
    }
    while (true)
    {
      if (!(localIterator.hasNext()))
        return localArrayList;
      localArrayList.add(((Header)localIterator.next()).getValue());
    }
  }

  public void addConfigurationListener(NodeConfigListener paramNodeConfigListener)
  {
    NodeConfigTranslator localNodeConfigTranslator = new NodeConfigTranslator(this, paramNodeConfigListener);
    this.configEventToListenerMap.put(paramNodeConfigListener, localNodeConfigTranslator);
    this.con.addPacketListener(localNodeConfigTranslator, new EventContentFilter(this, EventElementType.configuration.toString()));
  }

  public void addItemDeleteListener(ItemDeleteListener paramItemDeleteListener)
  {
    ItemDeleteTranslator localItemDeleteTranslator = new ItemDeleteTranslator(this, paramItemDeleteListener);
    this.itemDeleteToListenerMap.put(paramItemDeleteListener, localItemDeleteTranslator);
    EventContentFilter localEventContentFilter1 = new EventContentFilter(this, EventElementType.items.toString(), "retract");
    EventContentFilter localEventContentFilter2 = new EventContentFilter(this, EventElementType.purge.toString());
    this.con.addPacketListener(localItemDeleteTranslator, new OrFilter(localEventContentFilter1, localEventContentFilter2));
  }

  public void addItemEventListener(ItemEventListener paramItemEventListener)
  {
    ItemEventTranslator localItemEventTranslator = new ItemEventTranslator(this, paramItemEventListener);
    this.itemEventToListenerMap.put(paramItemEventListener, localItemEventTranslator);
    this.con.addPacketListener(localItemEventTranslator, new EventContentFilter(this, EventElementType.items.toString(), "item"));
  }

  protected PubSub createPubsubPacket(IQ.Type paramType, PacketExtension paramPacketExtension)
  {
    return createPubsubPacket(paramType, paramPacketExtension, null);
  }

  protected PubSub createPubsubPacket(IQ.Type paramType, PacketExtension paramPacketExtension, PubSubNamespace paramPubSubNamespace)
  {
    return PubSubManager.createPubsubPacket(this.to, paramType, paramPacketExtension, paramPubSubNamespace);
  }

  public DiscoverInfo discoverInfo()
    throws XMPPException
  {
    DiscoverInfo localDiscoverInfo = new DiscoverInfo();
    localDiscoverInfo.setTo(this.to);
    localDiscoverInfo.setNode(getId());
    return ((DiscoverInfo)SyncPacketSend.getReply(this.con, localDiscoverInfo));
  }

  public String getId()
  {
    return this.id;
  }

  public ConfigureForm getNodeConfiguration()
    throws XMPPException
  {
    return NodeUtils.getFormFromPacket(sendPubsubPacket(IQ.Type.GET, new NodeExtension(PubSubElementType.CONFIGURE_OWNER, getId()), PubSubNamespace.OWNER), PubSubElementType.CONFIGURE_OWNER);
  }

  public SubscribeForm getSubscriptionOptions(String paramString)
    throws XMPPException
  {
    return getSubscriptionOptions(paramString, null);
  }

  public SubscribeForm getSubscriptionOptions(String paramString1, String paramString2)
    throws XMPPException
  {
    return new SubscribeForm(((FormNode)((PubSub)sendPubsubPacket(IQ.Type.GET, new OptionsExtension(paramString1, getId(), paramString2))).getExtension(PubSubElementType.OPTIONS)).getForm());
  }

  public List<Subscription> getSubscriptions()
    throws XMPPException
  {
    return ((SubscriptionsExtension)((PubSub)sendPubsubPacket(IQ.Type.GET, new NodeExtension(PubSubElementType.SUBSCRIPTIONS, getId()))).getExtension(PubSubElementType.SUBSCRIPTIONS)).getSubscriptions();
  }

  public void removeConfigurationListener(NodeConfigListener paramNodeConfigListener)
  {
    PacketListener localPacketListener = (PacketListener)this.configEventToListenerMap.remove(paramNodeConfigListener);
    if (localPacketListener != null)
      this.con.removePacketListener(localPacketListener);
  }

  public void removeItemDeleteListener(ItemDeleteListener paramItemDeleteListener)
  {
    PacketListener localPacketListener = (PacketListener)this.itemDeleteToListenerMap.remove(paramItemDeleteListener);
    if (localPacketListener != null)
      this.con.removePacketListener(localPacketListener);
  }

  public void removeItemEventListener(ItemEventListener paramItemEventListener)
  {
    PacketListener localPacketListener = (PacketListener)this.itemEventToListenerMap.remove(paramItemEventListener);
    if (localPacketListener != null)
      this.con.removePacketListener(localPacketListener);
  }

  public void sendConfigurationForm(Form paramForm)
    throws XMPPException
  {
    PubSub localPubSub = createPubsubPacket(IQ.Type.SET, new FormNode(FormNodeType.CONFIGURE_OWNER, getId(), paramForm), PubSubNamespace.OWNER);
    SyncPacketSend.getReply(this.con, localPubSub);
  }

  protected Packet sendPubsubPacket(IQ.Type paramType, NodeExtension paramNodeExtension)
    throws XMPPException
  {
    return PubSubManager.sendPubsubPacket(this.con, this.to, paramType, paramNodeExtension);
  }

  protected Packet sendPubsubPacket(IQ.Type paramType, NodeExtension paramNodeExtension, PubSubNamespace paramPubSubNamespace)
    throws XMPPException
  {
    return PubSubManager.sendPubsubPacket(this.con, this.to, paramType, paramNodeExtension, paramPubSubNamespace);
  }

  public void setTo(String paramString)
  {
    this.to = paramString;
  }

  public Subscription subscribe(String paramString)
    throws XMPPException
  {
    return ((Subscription)((PubSub)sendPubsubPacket(IQ.Type.SET, new SubscribeExtension(paramString, getId()))).getExtension(PubSubElementType.SUBSCRIPTION));
  }

  public Subscription subscribe(String paramString, SubscribeForm paramSubscribeForm)
    throws XMPPException
  {
    PubSub localPubSub = createPubsubPacket(IQ.Type.SET, new SubscribeExtension(paramString, getId()));
    localPubSub.addExtension(new FormNode(FormNodeType.OPTIONS, paramSubscribeForm));
    return ((Subscription)((PubSub)PubSubManager.sendPubsubPacket(this.con, paramString, IQ.Type.SET, localPubSub)).getExtension(PubSubElementType.SUBSCRIPTION));
  }

  public String toString()
  {
    return super.toString() + " " + super.getClass().getName() + " id: " + this.id;
  }

  public void unsubscribe(String paramString)
    throws XMPPException
  {
    unsubscribe(paramString, null);
  }

  public void unsubscribe(String paramString1, String paramString2)
    throws XMPPException
  {
    sendPubsubPacket(IQ.Type.SET, new UnsubscribeExtension(paramString1, getId(), paramString2));
  }

  class EventContentFilter
  implements PacketFilter
  {
    private String firstElement;
    private String secondElement;

    EventContentFilter(, String paramString)
    {
      this.firstElement = paramString;
    }

    EventContentFilter(, String paramString1, String paramString2)
    {
      this.firstElement = paramString1;
      this.secondElement = paramString2;
    }

    public boolean accept()
    {
      int i;
      if (!(paramPacket instanceof Message))
        i = 0;
      while (true)
      {
        while (true)
        {
          NodeExtension localNodeExtension;
          while (true)
          {
            while (true)
            {
              while (true)
              {
                EventElement localEventElement;
                while (true)
                {
                  return i;
                  localEventElement = (EventElement)paramPacket.getExtension("event", PubSubNamespace.EVENT.getXmlns());
                  if (localEventElement != null)
                    break;
                  i = 0;
                }
                localNodeExtension = localEventElement.getEvent();
                if (localNodeExtension != null)
                  break;
                i = 0;
              }
              if (!(localNodeExtension.getElementName().equals(this.firstElement)))
                break label164;
              if (localNodeExtension.getNode().equals(this.this$0.getId()))
                break;
              i = 0;
            }
            if (this.secondElement != null)
              break;
            i = 1;
          }
          if (!(localNodeExtension instanceof EmbeddedPacketExtension))
            break;
          List localList = ((EmbeddedPacketExtension)localNodeExtension).getExtensions();
          if ((localList.size() <= 0) || (!(((PacketExtension)localList.get(0)).getElementName().equals(this.secondElement))))
            break;
          i = 1;
        }
        label164: i = 0;
      }
    }
  }

  public class ItemDeleteTranslator
  implements PacketListener
  {
    private ItemDeleteListener listener;

    public ItemDeleteTranslator(, ItemDeleteListener paramItemDeleteListener)
    {
      this.listener = paramItemDeleteListener;
    }

    public void processPacket()
    {
      EventElement localEventElement = (EventElement)paramPacket.getExtension("event", PubSubNamespace.EVENT.getXmlns());
      if (((PacketExtension)localEventElement.getExtensions().get(0)).getElementName().equals(PubSubElementType.PURGE_EVENT.getElementName()))
      {
        this.listener.handlePurge();
        return;
      }
      ItemsExtension localItemsExtension = (ItemsExtension)localEventElement.getEvent();
      List localList = localItemsExtension.getItems();
      Iterator localIterator = localList.iterator();
      ArrayList localArrayList = new ArrayList(localList.size());
      while (true)
      {
        while (!(localIterator.hasNext()))
        {
          ItemDeleteEvent localItemDeleteEvent = new ItemDeleteEvent(localItemsExtension.getNode(), localArrayList, Node.access$0(paramPacket));
          this.listener.handleDeletedItems(localItemDeleteEvent);
        }
        localArrayList.add(((RetractItem)localIterator.next()).getId());
      }
    }
  }

  public class ItemEventTranslator
  implements PacketListener
  {
    private ItemEventListener listener;

    public ItemEventTranslator(, ItemEventListener paramItemEventListener)
    {
      this.listener = paramItemEventListener;
    }

    public void processPacket()
    {
      Date localDate;
      ItemsExtension localItemsExtension = (ItemsExtension)((EventElement)paramPacket.getExtension("event", PubSubNamespace.EVENT.getXmlns())).getEvent();
      DelayInformation localDelayInformation = (DelayInformation)paramPacket.getExtension("delay", "urn:xmpp:delay");
      if (localDelayInformation == null)
        localDelayInformation = (DelayInformation)paramPacket.getExtension("x", "jabber:x:delay");
      String str = localItemsExtension.getNode();
      List localList1 = localItemsExtension.getItems();
      List localList2 = Node.access$0(paramPacket);
      if (localDelayInformation == null)
        localDate = null;
      while (true)
      {
        ItemPublishEvent localItemPublishEvent = new ItemPublishEvent(str, localList1, localList2, localDate);
        this.listener.handlePublishedItems(localItemPublishEvent);
        return;
        localDate = localDelayInformation.getStamp();
      }
    }
  }

  public class NodeConfigTranslator
  implements PacketListener
  {
    private NodeConfigListener listener;

    public NodeConfigTranslator(, NodeConfigListener paramNodeConfigListener)
    {
      this.listener = paramNodeConfigListener;
    }

    public void processPacket()
    {
      ConfigurationEvent localConfigurationEvent = (ConfigurationEvent)((EventElement)paramPacket.getExtension("event", PubSubNamespace.EVENT.getXmlns())).getEvent();
      this.listener.handleNodeConfiguration(localConfigurationEvent);
    }
  }
}